﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.IO;
using System.Text;

namespace BSC_OTC_THONGBAO.Code
{
    public class CL_Ultility
    {

        StreamWriter VietFileText;

        public void CREATE_LOG_FILE(string Log_type)
        {
            try
            {
                //Log_type LOG
                //Log_type ERR
                string File_name = Current_DATE("yyyymmdd");
                string v_AppPath = System.Reflection.Assembly.GetEntryAssembly().Location;
                string log_folder = v_AppPath + "Logs";
                string file_path = "";
                if (Log_type == "LOG")
                {
                    file_path = log_folder + "\\" + File_name + "_LOG.log";
                }
                else if (Log_type == "ERR")
                {
                    file_path = log_folder + "\\" + File_name + "_ERR.log";
                }
                if (File.Exists(file_path) == false)
                {
                    if (Directory.Exists(log_folder) == false)
                    {
                        Directory.CreateDirectory(log_folder);
                    }
                    VietFileText = File.CreateText(file_path);
                    VietFileText.Close();
                }
            }
            catch (Exception ex)
            {

            }

        }

        public string So_chuan(double so)
        {
            try
            {
                string so1 = "";
                if (so < 10)
                {
                    so1 = "0" + so.ToString();
                }
                else
                {
                    so1 = so.ToString();
                }
                return so1.ToString();
            }
            catch (Exception ex)
            {
                return "";
            }

        }

        public string Bon_So_chuan(string so)
        {
            string kq = "";
            try
            {
                int l = so.Length;
                switch (l)
                {
                    case 1:
                        kq = "000" + so.ToString();
                        break;
                    case 2:
                        kq = "00" + so.ToString();
                        break;
                    case 3:
                        kq = "0" + so.ToString();
                        break;
                    case 4:
                        kq = so.ToString();
                        break;
                }
                return kq.ToString();
            }
            catch (Exception ex)
            {
                //Log_event("Bon_So_chuan", ex.Message);
                return "";
            }
        }

        public string Current_DATE(string Date_type)
        {
            DateTime dt = System.DateTime.Now;
            string F_Time = dt.ToShortDateString();
            try
            {
                switch (Date_type.ToLower())
                {
                    case "yyyy_mm_dd":
                        F_Time = So_chuan(dt.Year).ToString() + "_" + So_chuan(dt.Month).ToString() + "_" + So_chuan(dt.Day).ToString();
                        break;
                    case "yyyymmdd":
                        F_Time = So_chuan(dt.Year).ToString() + So_chuan(dt.Month).ToString() + So_chuan(dt.Day).ToString();
                        break;
                    case "dd/mm/yyyy hh:pp:ss":
                        F_Time = So_chuan(dt.Day).ToString() + "/" + So_chuan(dt.Month).ToString() + "/" + So_chuan(dt.Year).ToString() + " " + So_chuan(dt.Hour).ToString() + ":" + So_chuan(dt.Minute).ToString() + ":" + So_chuan(dt.Second).ToString();
                        break;
                    case "hh:mm:ss:ms":
                        F_Time = So_chuan(dt.Hour).ToString() + ":" + So_chuan(dt.Minute).ToString() + ":" + So_chuan(dt.Second).ToString() + ":" + Bon_So_chuan(dt.Millisecond.ToString()).ToString();
                        break;
                    //case "HH:mm:ss":
                    case "hh:mm:ss":
                        F_Time = So_chuan(dt.Hour).ToString() + ":" + So_chuan(dt.Minute).ToString() + ":" + So_chuan(dt.Second).ToString();
                        break;
                    //case "dd/MM/yyyy":
                    case "dd/mm/yyyy":
                        F_Time = So_chuan(dt.Day).ToString() + "/" + So_chuan(dt.Month).ToString() + "/" + So_chuan(dt.Year).ToString();
                        break;
                }
                return F_Time;
            }
            catch (Exception ex)
            {
                //Log_event("Current_DATE", ex.Message);
                return "";
            }
        }


        public void UPDATE_LOG_FILE(string pv_logtype, string PV_TLID, string p_Sukien, string p_hamgoi, string Err_str)
        {
            try
            {

                //Log_type LOG
                //Log_type ERR
                string File_name = Current_DATE("yyyymmdd");
                string v_AppPath = System.Reflection.Assembly.GetEntryAssembly().Location;
                string log_folder = v_AppPath + "Logs";
                string file_path = "";
                if (pv_logtype == "LOG")
                {
                    file_path = log_folder + "\\" + File_name + "_LOG.log";
                }
                else if (pv_logtype == "ERR")
                {
                    file_path = log_folder + "\\" + File_name + "_ERR.log";
                }

                if (File.Exists(file_path) == false)
                {
                    CREATE_LOG_FILE(pv_logtype);
                }
                //--------------------------------------
                VietFileText = File.AppendText(file_path);
                VietFileText.WriteLine("====" + PV_TLID + "===" + Current_DATE("dd/mm/yyyy hh:pp:ss") + "========================================================");
                VietFileText.WriteLine("  Sự kiện : " + p_Sukien);
                VietFileText.WriteLine("  Hàm gọi : " + p_hamgoi);
                VietFileText.WriteLine("  Logtype : " + pv_logtype);
                VietFileText.WriteLine("  ex.tostring : " + Err_str);
                VietFileText.WriteLine("-------------------------------------------------------------------------------------------------");
                VietFileText.WriteLine("  ");
                VietFileText.Close();

            }
            catch (Exception ex)
            {

            }
        }

    }
}